package com.airS.task.fileMgr.service.gk.impl;

import com.airS.task.fileMgr.common.page.Page;
import com.airS.task.fileMgr.example.gk.EmployeeRoleExample;
import com.airS.task.fileMgr.mapper.gk.EmployeeRoleMapper;
import com.airS.task.fileMgr.model.gk.Employee;
import com.airS.task.fileMgr.model.gk.EmployeeRole;
import com.airS.task.fileMgr.model.gk.Role;
import com.airS.task.fileMgr.query.gk.EmployeeRoleQuery;
import com.airS.task.fileMgr.service.gk.EmployeeRoleService;
import com.airS.task.fileMgr.service.gk.EmployeeService;
import com.airS.task.fileMgr.service.gk.RoleService;
import com.airS.task.fileMgr.utils.page.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by alan on 2017/3/14.
 */
@Service
public class EmployeeRoleServiceImpl implements EmployeeRoleService {

    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private EmployeeService employeeService;

    @Override
    public EmployeeRole queryEmployeeRoleById(Integer id) {
        return employeeRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public Page<EmployeeRole> queryEmployeeRoles(EmployeeRoleQuery query) {
        PageUtil.startPage(query);
        List<EmployeeRole> employeeRoles = employeeRoleMapper.selectByExample(assemblyEmployeeRoleExample(query));
        Map<Integer, Role> roles = roleService.getAllRoleToMap();
        Map<Integer, Employee> employees = employeeService.getAllEmployeeToMap();
        for(EmployeeRole employeeRole : employeeRoles){
            Role role = roles.get(employeeRole.getRoleId());
            Employee employee = employees.get(employeeRole.getEmployeeId());
            employeeRole.setRoleName(role == null ? "未定义" : role.getRoleName());
            employeeRole.setEmployeeName(employee == null ? "未定义" : employee.getEmployeeName());
        }
        return new Page<EmployeeRole>(employeeRoles);
    }

    @Override
    public Integer addEmployeeRole(EmployeeRole employeeRole) {
        employeeRoleMapper.insert(employeeRole);
        return employeeRole.getEmployeeRoleId();
    }

    @Override
    public Integer updateEmployeeRoleById(EmployeeRole employeeRole) {
        return employeeRoleMapper.updateByPrimaryKeySelective(employeeRole);
    }

    @Override
    public Integer delEmployeeRoleById(Integer id) {
        return employeeRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer delEmployeeRoleByIds(List<Integer> ids) {
        EmployeeRoleExample example = new EmployeeRoleExample();
        EmployeeRoleExample.Criteria criteria = example.createCriteria();
        criteria.andEmployeeRoleIdIn(ids);
        return employeeRoleMapper.deleteByExample(example);
    }

    private EmployeeRoleExample assemblyEmployeeRoleExample(EmployeeRoleQuery query){
        EmployeeRoleExample example = new EmployeeRoleExample();
        EmployeeRoleExample.Criteria criteria = example.createCriteria();
        if(query.getEmployeeId() != null){
            criteria.andEmployeeIdEqualTo(query.getEmployeeId());
        }
        if(query.getRoleId() != null){
            criteria.andRoleIdEqualTo(query.getRoleId());
        }
        example.setOrderByClause("create_time desc");
        return example;
    }
}
